package com.example.taobaounion.presenter.impl;

import com.example.taobaounion.model.Api;
import com.example.taobaounion.model.bean.OnSellContent;
import com.example.taobaounion.presenter.IOnSellPagePresenter;
import com.example.taobaounion.utils.LogUtils;
import com.example.taobaounion.utils.RetrofitManager;
import com.example.taobaounion.utils.UrlUtils;
import com.example.taobaounion.view.IOnSellPageCallback;
import java.net.HttpURLConnection;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class OnSellPagePresenterImpl implements IOnSellPagePresenter {

    public static final int DEFAULT_PAGE=1;
    private int mCurrentPage=DEFAULT_PAGE;
    private IOnSellPageCallback mOnSellPageCallback=null;
    private final Api mApi;
    /**
     * 当前加载状态
     */
    private boolean mIsLoading=false;
    public OnSellPagePresenterImpl(){
        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        mApi = retrofit.create(Api.class);
    }
    @Override
    public void getOnSellContent() {
        LogUtils.d(this,"mIsLoading--->"+mIsLoading);
        if(mIsLoading){//进行防御，防止多次调用 如果执行了多次getOnSellContent，会loading多次
            return;
        }
        mIsLoading=true;
        //通知UI状态为加载中
        if (mOnSellPageCallback != null) {
            mOnSellPageCallback.onLoading();
        }
        //获取特惠内容

        String targetUrl = UrlUtils.getOnSellPageUrl(mCurrentPage);
        Call<OnSellContent> task = mApi.getOnSellPageContent(targetUrl);
        task.enqueue(new Callback<OnSellContent>() {
            @Override
            public void onResponse(Call<OnSellContent> call, Response<OnSellContent> response) {
                mIsLoading=false;
                int code=response.code();
                if(code== HttpURLConnection.HTTP_OK){
                    OnSellContent result = response.body();
                    //LogUtils.d(OnSellPagePresenterImpl.this,"result-->"+result);
                    onSuccess(result);
                }else {
                    onError();
                }
            }

            @Override
            public void onFailure(Call<OnSellContent> call, Throwable t) {
                LogUtils.d(OnSellPagePresenterImpl.this,"OnSellPagePresenterImpl-->"+t.toString());
                onError();
            }
        });
    }

    private void onSuccess(OnSellContent result) {
        if (mOnSellPageCallback != null) {
            try {
                if(isEmpty(result)){
                    onEmpty();
                }else {
                    mOnSellPageCallback.onContentLoadedSuccess(result);
                }
            }catch (Exception e){
                e.printStackTrace();
                onEmpty();
            }


        }
    }

    public boolean isEmpty(OnSellContent content){
        //LogUtils.d(this,"content.getData().getTbk_dg_optimus_material_response().getResult_list()"+content.getData().getTbk_dg_optimus_material_response().getResult_list());
        int size=content.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data().size();
        return size==0;

    }

    private void onEmpty() {
        if (mOnSellPageCallback != null) {
            mOnSellPageCallback.onEmpty();
        }
    }

    private void onError() {
        mIsLoading=false;
        if (mOnSellPageCallback != null) {
            mOnSellPageCallback.onError();
        }
    }

    @Override
    public void reload() {

    }



    @Override
    public void loaderMore() {
        if(mIsLoading){
            return;
        }
        mIsLoading=true;
        //加载更多
        mCurrentPage++;
        //去加载更多内容
        String targetUrl = UrlUtils.getOnSellPageUrl(mCurrentPage);
        Call<OnSellContent> task = mApi.getOnSellPageContent(targetUrl);
        task.enqueue(new Callback<OnSellContent>() {
            @Override
            public void onResponse(Call<OnSellContent> call, Response<OnSellContent> response) {
                mIsLoading=false;
                int code=response.code();
                if(code==HttpURLConnection.HTTP_OK){
                    OnSellContent result = response.body();
                    onLoadMore(result);
                }else {
                    onLoadMoreError();
                }
            }

            @Override
            public void onFailure(Call<OnSellContent> call, Throwable t) {
                   onLoadMoreError();
            }
        });

    }

    private void onLoadMoreError() {
        mIsLoading=false;
        mCurrentPage--;
        if (mOnSellPageCallback != null) {
            mOnSellPageCallback.onMoreLoadedError();
        }
    }

    private void onLoadMore(OnSellContent result) {
        if(result.getData().getTbk_dg_optimus_material_response()!=null){
            if (mOnSellPageCallback != null) {
                if(isEmpty(result)){
                    //LogUtils.d(this,"加载回来后为空");
                    mCurrentPage--;
                    mOnSellPageCallback.onMoreLoadedEmpty();

                }else {
                    //LogUtils.d(this,"加载回来后为有数据"+mCurrentPage);
                    mOnSellPageCallback.onMoreLoaded(result);

                }
            }
        }else {
            if(mOnSellPageCallback!=null){
                //LogUtils.d(this,"为加载回来后数据为空"+mCurrentPage);
                mOnSellPageCallback.onMoreLoadedEmpty();
            }
        }

    }

    @Override
    public void registerViewCallback(IOnSellPageCallback callback) {
      this.mOnSellPageCallback=callback;
    }

    @Override
    public void unregisterViewCallback(IOnSellPageCallback callback) {
      this.mOnSellPageCallback=null;
    }
}
